#include <utils/memutils.h>

void *memmove(void *dest, const void *src, size_t n) {
	uint8_t *pdest = (uint8_t *)dest;
	const uint8_t *psrc = (const uint8_t *)src;

	if(pdest < psrc) {
		while(n--)
			*pdest++ = *psrc++;
	} else {
		pdest += n;
		psrc += n;
		while(n--)
			*--pdest = *--psrc;
	}

	return dest;
}

void *memcpy(void *dest, const void *src, size_t n) {
	uint8_t *pdest = (uint8_t *)dest;
	const uint8_t *psrc = (const uint8_t *)src;

	while(n--)
		*pdest++ = *psrc++;

	return dest;
}

int memcmp(const void *s1, const void *s2, size_t n) {
	const uint8_t *p1 = (const uint8_t *)s1;
	const uint8_t *p2 = (const uint8_t *)s2;

	while(n--) {
		if(*p1 != *p2)
			return *p1 - *p2;
		p1++;
		p2++;
	}

	return 0;
}

void *memset(void *s, int c, size_t n) {
	uint8_t *p = (uint8_t *)s;
	uint8_t val = (uint8_t)c;

	while(n--)
		*p++ = val;

	return s;
}
